package com.vkdasari.youtube.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;

import com.vkdasari.youtube.model.Video;
import com.vkdasari.youtube.ui.action.AddFileAction;
import com.vkdasari.youtube.ui.action.RemoveFileAction;
import com.vkdasari.youtube.ui.action.StartUploadAction;
import com.vkdasari.youtube.ui.model.VideoUploadTableModel;
import com.vkdasari.youtube.util.FileUtil;
import com.vkdasari.youtube.util.ZebraConstants;

public class MainPanel extends JPanel{

	static private MainPanel instance=null;
	
	static public synchronized MainPanel getInstance(){
		if(instance == null){
			instance = new MainPanel();
		}
		return instance;
	}

	private static final long serialVersionUID = 1L;
	
	JToolBar toolBar;
	
	// Buttons
	JButton homeButton;
	JButton addFilesButton;
	JButton removeFilesButton;
	JButton startUploadsButton;
	JButton pauseUploadsButton;
	JButton stopUploadsButton;
	
	// Uploads TableModel
	private VideoUploadTableModel uploadsTableModel = new VideoUploadTableModel();
	
	// Uploads Table
	JTable uploadTable = new JTable(uploadsTableModel);
	
	public MainPanel(){
		super(new BorderLayout());
		createButtons();
		createToolBar();
		createUploadsPanel();
	}
	
	/**
	 * @return the uploadsTableModel
	 */
	public VideoUploadTableModel getUploadsTableModel() {
		return uploadsTableModel;
	}
	
	
	/**
	 * @return the uploadTable
	 */
	public JTable getUploadTable() {
		return uploadTable;
	}
	

	/**
	 * Create Buttons
	 */
	private void createButtons(){
		Dimension buttonPreferredSize = new Dimension(60, 30);
		homeButton = new JButton("Home", new ImageIcon(getClass().getClassLoader().getResource("icon/Home_16.png")));
		homeButton.setPreferredSize(buttonPreferredSize);
		
		addFilesButton = new JButton();
		addFilesButton.setAction(new AddFileAction("Add Files", new ImageIcon(getClass().getClassLoader().getResource("icon/AddFile_16.png"))));
		addFilesButton.setPreferredSize(buttonPreferredSize);
		
		removeFilesButton = new JButton();
		removeFilesButton.setAction(new RemoveFileAction("Remove Files", new ImageIcon(getClass().getClassLoader().getResource("icon/RemoveFile_16.png"))));
		removeFilesButton.setPreferredSize(buttonPreferredSize);
		
		startUploadsButton = new JButton();
		startUploadsButton.setAction(new StartUploadAction("Start Upload", new ImageIcon(getClass().getClassLoader().getResource("icon/UploadFile_16.png"))));
		startUploadsButton.setPreferredSize(buttonPreferredSize);
		
		pauseUploadsButton = new JButton("Pause", new ImageIcon(getClass().getClassLoader().getResource("icon/Pause_16.png")));
		pauseUploadsButton.setPreferredSize(buttonPreferredSize);
		
		
		stopUploadsButton = new JButton("Stop", new ImageIcon(getClass().getClassLoader().getResource("icon/Stop_16.png")));
		stopUploadsButton.setPreferredSize(buttonPreferredSize);
		
	}
	
	
	/**
	 * Create ToolBar
	 */
	private void createToolBar(){

		// Create Tool Bar
		toolBar = new JToolBar("Zebra Tools");
		toolBar.setFloatable(false);
		
		add(toolBar, BorderLayout.PAGE_START);	
		
		// Add Buttons to ToolBar	
		toolBar.add(homeButton);
		toolBar.add(addFilesButton);
		toolBar.add(removeFilesButton);
		toolBar.add(startUploadsButton);
		toolBar.add(pauseUploadsButton);
		toolBar.add(stopUploadsButton);

	}
	
	
	private void createUploadsPanel(){
		
		JScrollPane scrollPane = new JScrollPane(uploadTable);
		add(scrollPane);
		
		// Load Files
//		String sourceFolder = "C:\\Users\\VDasari\\Videos\\Upload";
//		File dir = new File(sourceFolder);
//		for (File child : dir.listFiles()) {
//			if (".".equals(child.getName()) || "..".equals(child.getName())) {
//				continue; // Ignore the self and parent aliases.
//			}
//			Video video = new Video();
//			video.setName(child.getName());
//			video.setFilelocation(child.getAbsolutePath());
//			video.setSize(FileUtil.getSize(child.length()));
//			uploadsTableModel.addRow(video);
//			
//		}
		
	}
	

	
	
}
